/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.nyquistmeteocal.control;

import it.nyquistmeteocal.entity.Event;
import it.nyquistmeteocal.boundary.EventManager;
import it.nyquistmeteocal.entity.Notification;
import it.nyquistmeteocal.boundary.NotificationManager;
import it.nyquistmeteocal.entity.UserNotification;
import it.nyquistmeteocal.boundary.UserNotificationManager;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Stateful;
import javax.ejb.Stateless;

/**
 *
 * @author gatb27
 */
@Stateless
public class Weatherchecker {
    
    @EJB
    private EventManager em;
    @EJB
    private NotificationManager nm;
    @EJB
    private UserNotificationManager unm;
   
    private Event currentEvent;
    private String weather;
    private String sqlQuery;
    private String text;
    private Integer i;
    private Integer count=0;
    private Notification tmrrainy = new Notification();
    private Notification badweather = new Notification();
    private Integer notificationid;
    private UserNotification usernotification = new UserNotification();
    private List<String> Recipients = new ArrayList<String>();
    private List<Event> rainyEvents = new ArrayList<Event>();
    private List<Event> tmrRainyEvents = new ArrayList<Event>();
    private List<Event> badWeatherEvents = new ArrayList<Event>();
    
    
    
    
    //@Schedule(second = "*/30", minute = "*", hour = "*", info="Every 30 seconds", persistent=false)
    //public void atSchedule() {
    //System.out.println("------> I'm the Timer Bean and I'm working <------");
        
    public void checkTomorrowRainy() {
        rainyEvents = em.getBadEvents();
        tmrRainyEvents = em.getTomorrowRainyEvents();
        Integer count = rainyEvents.size();
        System.out.println("count before iterator IS:" + count + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
        count = tmrRainyEvents.size();
        System.out.println("count before iterator IS:" + count + "###############################");
        int i;
        
        
        if(count != 0){
            System.out.println("-------------I'M IN THE FIRST IF-------------");
            for(i=0; i<count; i++) {
                System.out.println("-----I'M IN THE FIRST FOR NUMBER" + i + "--------");
                Event currentEvent = tmrRainyEvents.get(i);
                Recipients = nm.getRecipients2(currentEvent.getId());
                Integer countt = Recipients.size();
                System.out.println("count of recipient IS:" + countt + "***********************");
                
                if(countt != 0) {
                    System.out.println("-------------I'M IN THE SECOND IF-------------");
                    text="The outdoor event " + currentEvent.getEventname() + 
                        " scheduled for tomorrow will have RAINY weather!";
                    tmrrainy = new Notification();
                    tmrrainy.setText(text);
                    tmrrainy.setEventid(currentEvent.getId());
                    tmrrainy.setType("tomorrowRainy");
                    System.out.println(tmrrainy.getEventid());
                    System.out.println(tmrrainy.getText());
                    System.out.println(tmrrainy.getType());
                    
                    nm.saveNotification(tmrrainy);
                    }
                    
                    notificationid = tmrrainy.getId();
                    
                    usernotification = new UserNotification();
                    usernotification.setNotificationid(notificationid);
                    int j;
                    for(j=0; j<countt; j++) {
                        System.out.println("-----I'M IN THE SECONDO FOR NUMBER" + j + "--------");
                        String currentRecipient = Recipients.get(j);
                        usernotification.setUsermail(currentRecipient);
                        System.out.println(usernotification.getUsermail());
                        unm.saveUserNotification(usernotification);
                    }
                }
            }
        }
    
    public void checkBadWeather() {

        rainyEvents = em.getBadEvents();

        badWeatherEvents = em.getBadWeatherEvents();
        Integer count = rainyEvents.size();
        System.out.println("count before iterator IS:" + count + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
        count = badWeatherEvents.size();
        System.out.println("count before iterator IS:" + count + "###############################");
        int i;

        if (count != 0) {
            System.out.println("-------------I'M IN THE FIRST IF-------------");
            for (i = 0; i < count; i++) {
                System.out.println("-----I'M IN THE FIRST FOR NUMBER" + i + "--------");
                Event currentEvent = badWeatherEvents.get(i);
                Recipients = nm.getRecipients3(currentEvent.getId());
                Integer countt = Recipients.size();
                System.out.println("count of recipient IS:" + countt + "***********************");

                if (countt != 0) {
                    System.out.println("-------------I'M IN THE SECOND IF-------------");
                    text = "The outdoor event " + currentEvent.getEventname()
                            + " scheduled in 3 days will have RAINY weather!"
                            + "Do you want to change to date to SOME DAY ?";
                    this.badweather.setText(text);
                    this.badweather.setEventid(currentEvent.getId());
                    this.badweather.setType("badWeather");
                    System.out.println(badweather.getEventid());
                    System.out.println(badweather.getText());
                    System.out.println(badweather.getType());

                    if (nm == null) {
                        System.out.println("IS NULL @@@");
                    }
                    nm.saveNotification(this.badweather);

                    notificationid = badweather.getId();
                    System.out.println(badweather.getId());

                    usernotification.setNotificationid(notificationid);
                    int j;
                    for (j = 0; j < countt; j++) {
                        System.out.println("-----I'M IN THE SECONDO FOR NUMBER" + j + "--------");
                        String currentRecipient = Recipients.get(j);
                        usernotification.setUsermail(currentRecipient);
                        System.out.println(usernotification.getUsermail());
                        System.out.println(usernotification.getNotificationid());
                        unm.saveUserNotification(usernotification);
                    }
                }
            }
        }

    }
        
    
    
}
